﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using Microsoft.Phone.Scheduler;

namespace Angerona.Views
{
    public partial class User : PhoneApplicationPage
    {

        public User()
        {
            InitializeComponent();
            ApplicationBarInitializer();
            ReadingUserData();
        }

        /// <summary>
        /// Crea l'ApplicationBar localizzata
        /// Create the localized ApplicationBar
        /// </summary>
        private void ApplicationBarInitializer()
        {
            // Set the page's ApplicationBar to a new instance of ApplicationBar.
            ApplicationBar = new ApplicationBar();

            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.Opacity = 1.0;
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = false;
            // edit button
            ApplicationBarIconButton btn_edit = new ApplicationBarIconButton();
            btn_edit.IconUri = new Uri("/Img/appbar_edit.png", UriKind.Relative);
            btn_edit.Text = AngeronaLang.edit;
            ApplicationBar.Buttons.Add(btn_edit);
            btn_edit.Click += new EventHandler(btn_edit_Click);

            //help button
            ApplicationBarIconButton btn_help = new ApplicationBarIconButton();
            btn_help.IconUri = new Uri("/Img/appbar_help.png", UriKind.Relative);
            btn_help.Text = AngeronaLang.help;
            ApplicationBar.Buttons.Add(btn_help);
            btn_help.Click += new EventHandler(btn_help_Click);

            // delete button
            ApplicationBarIconButton btn_del = new ApplicationBarIconButton();
            btn_del.IconUri = new Uri("/Img/appbar_delete.png", UriKind.Relative);
            btn_del.Text = AngeronaLang.del;
            ApplicationBar.Buttons.Add(btn_del);
            btn_del.Click += new EventHandler(btn_del_Click);
        }
        
        /// <summary>
        /// Legge i dati dai setting relativi all'utente
        /// Aggiorna i campi con i dati dell'utente
        /// Refresh the user data displayed
        /// </summary>
        private void ReadingUserData()
        {
            //reading name
            uname.Text = "";
            uname.Foreground = (SolidColorBrush) Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("uname"))
                uname.Text = IsolatedStorageSettings.ApplicationSettings["uname"] as string;

            //reading surname
            usurname.Text = "";
            usurname.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("usurname"))
                usurname.Text = IsolatedStorageSettings.ApplicationSettings["usurname"] as string;

            //reading License Number
            licensenum.Text = "";
            licensenum.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licensenum"))
                licensenum.Text = IsolatedStorageSettings.ApplicationSettings["licensenum"] as string;

            //reading License Point
            licensepoint.Text = "";
            licensepoint.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licensepoint"))
                licensepoint.Text = IsolatedStorageSettings.ApplicationSettings["licensepoint"] as string;

            //reading License Release
            DateTime lreleasedate = new DateTime(2013, 1, 1);
            licenserelease.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licenserelease"))
                lreleasedate = (DateTime)IsolatedStorageSettings.ApplicationSettings["licenserelease"];
            licenserelease.Text = String.Format(lreleasedate.ToString("d"));

            //reading License Expire
            DateTime lexpiredate = new DateTime(2013, 1, 1);
            licenseexpire.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licenseexpire"))
                lexpiredate = (DateTime)IsolatedStorageSettings.ApplicationSettings["licenseexpire"];
            licenseexpire.Text = String.Format(lexpiredate.ToString("d"));

            //reading License Category
            licensecategory.Text = "";
            licensecategory.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licensecategory"))
                licensecategory.Text = IsolatedStorageSettings.ApplicationSettings["licensecategory"] as string;

            //reading License Restriction
            licenserextriction.Text = "";
            licenserextriction.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            if (IsolatedStorageSettings.ApplicationSettings.Contains("licenserextriction"))
                licenserextriction.Text = IsolatedStorageSettings.ApplicationSettings["licenserextriction"] as string;
        }

        /// <summary>
        /// Handler del bottone modifica
        /// Handler of delete edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_edit_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ModUser.xaml", UriKind.Relative));

        }
        /// <summary>
        /// Handler del bottone elimina
        /// Handler of delete botton
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_del_Click(object sender, EventArgs e)
        {
            
            //Show messageBox for confirmation of deleting user data
            if (MessageBox.Show(AngeronaLang.areusure, AngeronaLang.deluserdata, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                ScheduledAction OldReminder = ScheduledActionService.Find("LicenseExpired");
                if (OldReminder != null)
                    ScheduledActionService.Remove(OldReminder.Name);

                IsolatedStorageSettings.ApplicationSettings.Remove("uname");
                IsolatedStorageSettings.ApplicationSettings.Remove("usurname");
                IsolatedStorageSettings.ApplicationSettings.Remove("licensenum");
                IsolatedStorageSettings.ApplicationSettings.Remove("licensepoint");
                IsolatedStorageSettings.ApplicationSettings.Remove("licenserelease");
                IsolatedStorageSettings.ApplicationSettings.Remove("licenseexpire");
                IsolatedStorageSettings.ApplicationSettings.Remove("licensecategory");
                IsolatedStorageSettings.ApplicationSettings.Remove("licenserextriction");
                ReadingUserData();
            }
        }


        private void btn_help_Click(object sender, EventArgs e)
        {
            MessageBox.Show(AngeronaLang.help_user, AngeronaLang.help, MessageBoxButton.OK);
        }

        /// <summary>
        /// Serve per fare il refresh della pagina quando da ModUser si clicca indietro (tasto fisico)
        /// Needed for refresh of the page when backbutton (phisical) is pressed in ModUser page
        /// </summary>
        /// <param name="e"></param>   
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                ReadingUserData();
        }
    }
}